/*
 * @lc app=leetcode.cn id=419 lang=cpp
 *
 * [419] 甲板上的战舰
 */
#include "include.h"
// @lc code=start
class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        rowNum_ = board.size();
        columnNum_ = board.at(0).size();

        vector<vector<unsigned char>> visited(rowNum_, vector<unsigned char>(columnNum_, 0));
// plotMatrix(board);
// plotMatrix(visited);
        for (int row=0;row<rowNum_;row++){
            for (int column=0;column<columnNum_;column++){
                checkCell(row, column, board, visited);
            }
        }

        return counter_;
    }
private:
    int rowNum_ = 0;
    int columnNum_ = 0;
    int counter_ = 0;

    void checkCell(int row, int column, const vector<vector<char>>& board, vector<vector<unsigned char>>& visited){
        if (visited[row][column] == 1){return;}
        if (board[row][column] == '.'){return;}
        counter_ += 1;
        dfs(row, column, board, visited);

// plotMatrix(visited);
    }
    void dfs(int row, int column, const vector<vector<char>>& board, vector<vector<unsigned char>>& visited){
        if (visited[row][column] == 1){return;}
        
        visited[row][column] = 1;
        if (insideBoard(row-1, column)) {
            if (board[row-1][column] == 'X') dfs(row-1, column, board, visited);
        };
        if (insideBoard(row+1, column)) {
            if (board[row+1][column] == 'X') dfs(row+1, column, board, visited);
        };
        if (insideBoard(row, column-1)) {
            if (board[row][column-1] == 'X') dfs(row, column-1, board, visited);
        };
        if (insideBoard(row, column+1)) {
            if (board[row][column+1] == 'X') dfs(row, column+1, board, visited);
        };
    }
    inline bool insideBoard(int row, int column){
        return row >= 0 and row < rowNum_ and column >= 0 and column < columnNum_;
    }

    void plotMatrix(const vector<vector<char>>& matrix){
        printf("-------------\n");
        for (int row=0;row<rowNum_;row++){
            for (int column=0;column<columnNum_;column++){
                printf("%c ", matrix[row][column]);
            }
            printf("\n");
        }
    }
    void plotMatrix(const vector<vector<unsigned char>>& matrix){
        printf("-------------\n");
        for (int row=0;row<rowNum_;row++){
            for (int column=0;column<columnNum_;column++){
                printf("%d ", matrix[row][column]);
            }
            printf("\n");
        }
    }

};
// @lc code=end

int main(){
    vector<vector<char>> board {
        {'X','.','.','X'},
        {'.','.','.','X'},
        {'.','.','.','X'}
    };
    Solution test;
    int res = test.countBattleships(board);

}